const Controller = require("egg").Controller;

let fruitList = [
    "香蕉",
    "苹果",
    "鸭梨"
]

class FruitsController extends Controller{

    async index(){
        // let query = this.ctx.request.query;
        this.ctx.body = fruitList;
    }

    async new(){
        this.ctx.body = `
            <form method="post" action="/fruits">
                <input name="fruitname">
                <button>提交</button>
            </form>
        `
    }
    async create(){
        let fruit = this.ctx.request.body;
        fruitList.push(fruit.fruitname)
        // this.ctx.body = "添加成功"
        this.ctx.redirect("/fruits")
    }

    async getId(){
        let id = this.ctx.params.id;
        this.ctx.body = `
            <h1>传递的ID为${id}</h1>
        `
    }
    // async createPage(){
    //     this.ctx.body = `
    //         <form method="post" action="/createfruit">
    //             <input name="fruitname">
    //             <button>提交</button>
    //         </form>
    //     `
    // }
    // async createFruit(){
    //     let fruit = this.ctx.request.body;
    //     fruitList.push(fruit.fruitname)
    //     this.ctx.body = "添加成功"
    // }

}

module.exports = FruitsController;